﻿Public Class frmPagIBIGBR
    Private frmParent As frmMain
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManager As New PayrollManager
    Private Sub frmPagIBIGBR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()

        Try
            'Dim pagIBIGList As List(Of PagIBIG) = sqlList.getAllPagIBIG()
            Dim pagIBIGList As List(Of PagIBIG) = payrollManager.getAllPagIBIG()
            For Each pagIBIG As PagIBIG In pagIBIGList
                Dim item As ListViewItem
                item = lvwPagIBIG.Items.Add(pagIBIG.min_salary)
                item.SubItems.Add(pagIBIG.max_salary)
                item.SubItems.Add(pagIBIG.employer_share)
                item.SubItems.Add(pagIBIG.employee_share)
                item.SubItems.Add(pagIBIG.min_share)
                item.SubItems.Add(pagIBIG.id)
                
            Next
            grpPagIBIG.Text = "Total Record(s): " + lvwPagIBIG.Items.Count.ToString()
            validator.resizes(lvwPagIBIG)
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        frmPagIBIGDE.ShowDialog()
        frmPagIBIGBR_Load(vbNull, EventArgs.Empty)
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpPagIBIG)
        btnAdd.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnImport.Enabled = True
        grpPagIBIG.Text = "Total Record(s): 0"
        validator.resizes(lvwPagIBIG)
        Clipboard.Clear()
        Return 0
    End Function

    Private Sub lvwPagIBIG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwPagIBIG.SelectedIndexChanged
        btnEdit.Enabled = (lvwPagIBIG.SelectedItems.Count = 1)
        btnDelete.Enabled = (lvwPagIBIG.SelectedItems.Count > 0)
    End Sub

    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
        Clipboard.SetText("Pag-Ibig")
        frmImport.ShowDialog()
        frmPagIBIGBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Clipboard.SetText(lvwPagIBIG.SelectedItems(0).SubItems(5).Text)
        frmPagIBIGDE.ShowDialog()
        frmPagIBIGBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            For i As Integer = 1 To lvwPagIBIG.SelectedItems.Count
                'sqlManager.executeQuery("DELETE FROM tbl_pagibig WHERE id = " + lvwPagIBIG.SelectedItems(i - 1).SubItems(5).Text)
                payrollManager.deleteItemPagIBIG(lvwPagIBIG.SelectedItems(i - 1).SubItems(5).Text)
            Next
            MsgBox("Record(s) has been deleted.")
            frmPagIBIGBR_Load(vbNull, EventArgs.Empty)
            grpPagIBIG.Text = "Total Record(s): " + lvwPagIBIG.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub
End Class